package service

import (
	"course/basic/config"
	"course/handler/model"
	"course/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddSlot(c *gin.Context) {
	var form request.AddSlot
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	var slot model.Slot
	if err := slot.IsSlot(config.DB, form.UserId, form.CourseId, form.SlotTime); err == nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "您已预约,请勿重复预约",
		})
		return
	}
	slot.UserId = form.UserId
	slot.CourseId = form.CourseId
	slot.SlotTime = form.SlotTime
	if err := slot.AddSlot(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "预约失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "预约成功",
	})
	return
}
